package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.DailyCommendQueryObject;
import cn.wolfcode.trip.base.service.IDailyCommendService;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("dailys")
public class DailyController {

    @Autowired
    private IDailyService dailyService;

    @Autowired
    private IDailyCommendService commendService;


    @GetMapping("/{id}")
    public Object get(@PathVariable Long id) {
        return dailyService.get(id);
    }

    @PostMapping
    public JSONResult save(Daily daily){
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            daily.setAuthor(user);
            dailyService.save(daily);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JSONResult update(Daily daily){
        JSONResult result = new JSONResult();
        try {
            dailyService.update(daily);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }



    @GetMapping("/dailyCommends")
    public Object getHotDailyCommend(@ModelAttribute("qo") DailyCommendQueryObject qo){
        return commendService.query(qo);
    }





}
